﻿using MC.Warehouse.Entity;
using MC.Warehouse.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using MC.Warehouse.Core;

namespace MC.Warehouse.DAL
{
    internal class CustomersRepository : Repository<Customer,CustomerModel>
    {
        public ICollectionView Search(CustomerModel customerModel)
        {
            SqlSelectCols = p => null;

            if (!string.IsNullOrEmpty(customerModel.Id))
            {
                SqlWhereCollection.Add( new Parameter<Customer>(p => p.Id, Oparator.Like, customerModel.Id));
            }
            if (!string.IsNullOrEmpty(customerModel.CompanyName))
            {
                SqlWhereCollection.Add( new Parameter<Customer>(p => p.CompanyName, Oparator.Like, 
                    customerModel.CompanyName));
            }
            if (!string.IsNullOrEmpty(customerModel.ContactName))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.ContactName, Oparator.Like, 
                    customerModel.ContactName));
            }
            if (!string.IsNullOrEmpty(customerModel.Country))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Country, Oparator.Like, 
                    customerModel.Country));
            }
            if (!string.IsNullOrEmpty(customerModel.Email))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Email, Oparator.Like, 
                    customerModel.Email));
            }
            if (!string.IsNullOrEmpty(customerModel.Fax))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Fax, Oparator.Like, 
                    customerModel.Fax));
            }
            if (!string.IsNullOrEmpty(customerModel.Address))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Address, Oparator.Like, 
                    customerModel.Address));
            }
            if (!string.IsNullOrEmpty(customerModel.Code))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Code, Oparator.Like, 
                    customerModel.Code));
            }
            if (!string.IsNullOrEmpty(customerModel.Phone))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Phone, Oparator.Like, 
                    customerModel.Phone));
            }
            if (!string.IsNullOrEmpty(customerModel.Region))
            {
                SqlWhereCollection.Add(new Parameter<Customer>(p => p.Region, Oparator.Like, 
                    customerModel.Region));
            }
            return
                CollectionViewSource.GetDefaultView(
                    JsonConvert.DeserializeObject<List<CustomerModel>>(JsonConvert.SerializeObject(Search
                    ())));
        }

        internal byte Insert(CustomerModel customerModel)
        {
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Id, Oparator.Equal, "MINH"));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.CompanyName, Oparator.Equal, 
                                                            customerModel.CompanyName));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.ContactName, Oparator.Equal, 
                                                            customerModel.ContactName));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Country, Oparator.Equal, 
                                                            customerModel.Country));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Email, Oparator.Equal, 
                customerModel.Email));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Fax, Oparator.Equal, 
                customerModel.Fax));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Address, Oparator.Equal, 
                                                                customerModel.Address));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Code, Oparator.Equal, 
                customerModel.Code));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Phone, Oparator.Equal, 
                customerModel.Phone));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Region, Oparator.Equal, 
                customerModel.Region));
            return Insert();
        }

        internal byte Update(CustomerModel customerModel)
        {
            SqlWhereCollection.Add(new Parameter<Customer>(p => p.Id,Oparator.Equal, customerModel.Id));

            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.CompanyName, Oparator.Equal,
                                                             customerModel.CompanyName));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.ContactName, Oparator.Equal,
                                                             customerModel.ContactName));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Country, Oparator.Equal, 
                                                            customerModel.Country));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Email, Oparator.Equal, 
                customerModel.Email));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Fax, Oparator.Equal, 
                customerModel.Fax));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Address, Oparator.Equal, 
                                                            customerModel.Address));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Code, Oparator.Equal, 
                customerModel.Code));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Phone, Oparator.Equal, 
                customerModel.Phone));
            SqlUpdateCollection.Add(new Parameter<Customer>(p => p.Region, Oparator.Equal, 
                customerModel.Region));
            return Update();
        }

        internal byte Delete(string id)
        {
            SqlSelectCols = p => null;
            SqlWhereCollection.Add(new Parameter<Customer>(p => p.Id, Oparator.Equal,id));
            return Delete();
        }

        internal CustomerModel FindById(string id)
        {
            SqlSelectCols = p => null;
            SqlWhereCollection.Add(new Parameter<Customer>(p => p.Id, Oparator.Equal, id));
            return GetById();
        }

        internal ICollectionView AutoLoad()
        {
            return CollectionViewSource.GetDefaultView
                (JsonConvert.DeserializeObject<List<CustomerModel>>(JsonConvert.SerializeObject(AutoSearch
                ())));
        }
    }
}
